﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.IO;

namespace FX.CC.Andon.EqKanban
{
    public class HttpFile
    {
        /// <summary>   
        /// 下载方法(带进度条)   
        /// </summary>   
        /// <param name="fileUrl">服务器URL地址</param>   
        /// <param name="targetFileName">存放到本地的路径</param>   
        /// <param name="progBar">进度条</param>   
        public static void Download(string fileUrl, string targetFileName/*, ProgressBar progBar*/)
        {

            try
            {
                HttpWebRequest webReq = (HttpWebRequest)HttpWebRequest.Create(fileUrl);
                using (HttpWebResponse webRsp = (HttpWebResponse)webReq.GetResponse())
                {
                    /*long totalBytes = webRsp.ContentLength;
                    progBar.Maximum = (int)totalBytes;*/
                    using (Stream rspStrm = webRsp.GetResponseStream())
                    {
                        using (Stream fileStrm = new FileStream(targetFileName, FileMode.Create))
                        {
                            /*long totalDownloadedByte = 0;*/
                            byte[] tmpBuff = new byte[1024];
                            int readLen = rspStrm.Read(tmpBuff, 0, (int)tmpBuff.Length);
                            while (readLen > 0)
                            {
                                /*totalDownloadedByte += readLen;*/
                                fileStrm.Write(tmpBuff, 0, readLen);
                                /*progBar.Value = (int)totalDownloadedByte;*/
                                readLen = rspStrm.Read(tmpBuff, 0, (int)tmpBuff.Length);
                            }
                            fileStrm.Close();
                            rspStrm.Close();
                        }
                    }
                }
            }

            catch (Exception ex)
            {
            }
        }
    }
}
